package com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.service;

import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.entity.DeviceListEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.entity.DeviceLogAddDto;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.entity.DeviceLogEntity;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.device.mapper.DeviceLogMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeviceLogService {

    @Autowired
    DeviceLogMapperImpl deviceLogMapper;
    private static final Logger logger = LoggerFactory.getLogger(DeviceListService.class);

    /**
     * 查询设备日志
     * @param tenant_id
     * @param name
     * @param type
     * @param opername
     * @param devicecode
     * @param devicename
     * @param status
     * @param page
     * @param pagesize
     * @return
     */
    public DataResult<DeviceLogEntity> getDeviceLog(String tenant_id, String name, String type, String opername,
                                                    String devicecode, String devicename, String status, Integer page, Integer pagesize) {
        DataResult<DeviceLogEntity> dataResult = new DataResult<>(0, "设备列表信息查询失败!");
        Integer startNumber = (page - 1) * pagesize;
        try {
            List<DeviceLogEntity> deviceLogEntities = new ArrayList<>();
            int deviceLogCount = deviceLogMapper.GetDeviceLogCount(tenant_id, name, type, opername, devicecode, devicename, status);
            deviceLogEntities = deviceLogMapper.GetDeviceLog(tenant_id, name, type, opername, devicecode, devicename, status, startNumber, pagesize);
            dataResult.setStatus(1);
            dataResult.setDataList(deviceLogEntities);
            dataResult.setTotalSize(deviceLogCount);
            dataResult.setMessage("查询成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 根据ID 查询设备记录
     *
     * @param tenantid tenantid
     * @param id       设备id
     * @return 返回设备信息
     */
    public DataResult<DeviceLogEntity> getDeviceLogById(String tenantid, String id) {
        DataResult<DeviceLogEntity> dataResult = new DataResult<>(0, "获取信息失败");
        try {
            if (id != null && id.length() > 0) {
                DeviceLogEntity deviceLogEntity = deviceLogMapper.GetDeviceLogById(tenantid, id);
                dataResult.setData(deviceLogEntity);
                dataResult.setStatus(1);
                dataResult.setMessage("查询成功");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 添加设备信息
     *
     * @param tenantid     tenantid
     * @param deviceLogAddDtos 添加设备列表
     * @return 返回格式化请求信息
     */
    public DataResult AddDeviceLog(String tenantid, List<DeviceLogAddDto> deviceLogAddDtos) {
        DataResult dataResult = new DataResult(0, "添加失败");
        List<DeviceLogEntity> deviceLogEntities = new ArrayList<>();
        try {
            if (deviceLogAddDtos != null && deviceLogAddDtos.size() > 0) {
                for (DeviceLogAddDto item : deviceLogAddDtos
                ) {
                    DeviceLogEntity deviceLog = new DeviceLogEntity();
                    deviceLog.setId(Long.parseLong(MyTool.getRandomId()));
                    deviceLog.setType(item.getType());
                    deviceLog.setName(item.getName());
                    deviceLog.setOpername(item.getOpername());
                    deviceLog.setOperuserid(item.getOperuserid());
                    deviceLog.setDevicename(item.getDevicename());
                    deviceLog.setDevicecode(item.getDevicecode());
                    deviceLog.setDeviceinfo(item.getDeviceinfo());
                    deviceLog.setStatus(item.getStatus());
                    deviceLog.setCreate_time(MyTool.GenerateTime("yyyy-MM-dd HH:mm:ss"));
                    deviceLog.setDr(0);
                    deviceLog.setTenant_id(tenantid);
                    deviceLog.setVersion(item.getVersion());
                    deviceLogEntities.add(deviceLog);
                }
                int count = deviceLogMapper.InsertDeviceLog(deviceLogEntities);
                String Message = "成功添加【 " + count + " 】条数据";
                dataResult.setMessage(Message);
                dataResult.setStatus(1);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 根据ID 删除
     *
     * @param id 设备id
     * @return 返回设备信息
     */
    public DataResult DeleteDeviceLog(String id) {
        DataResult<DeviceListEntity> dataResult = new DataResult<>(0, "删除失败");
        try {
            if (id != null && id.length() > 0) {
                int count = deviceLogMapper.DeleteDeviceLog(id);
                if (count != 0) {
                    dataResult.setStatus(1);
                    dataResult.setMessage("删除成功");
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 根据手持设备名清理记录
     * @param name
     * @param tenantId
     * @return
     */
    public DataResult DeleteDeviceLogByName(String name, String tenantId) {
        DataResult<DeviceListEntity> dataResult = new DataResult<>(0, "删除失败");
        try {
            if (!StringUtils.isBlank(name)) {
                int count = deviceLogMapper.DeleteDeviceLogByName(name, tenantId);
                if (count != 0) {
                    dataResult.setStatus(1);
                    dataResult.setMessage("删除成功");
                }else{
                    dataResult.setStatus(1);
                    dataResult.setMessage("操作成功");
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }

    /**
     * 更新device 信息
     *
     * @param id         id
     * @param deviceLog device
     * @return 返回结果
     */
    public DataResult ModifyDeviceLog(String id, DeviceLogEntity deviceLog) {
        DataResult dataResult = new DataResult(0, "修改失败");
        if (id == null || id.length() <= 0) return dataResult;
        if (deviceLog == null) return dataResult;
        try {
            if (deviceLog.getId() == 0) deviceLog.setId(Long.parseLong(id));
            int result = deviceLogMapper.ModifyDeviceLog(deviceLog);
            if (result == 1) {
                dataResult.setStatus(1);
                dataResult.setMessage("修改成功");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return dataResult;
    }




}
